package update_grammar;

import guard_grammar.Token;

import java.util.LinkedList;


/**
 * @author Zlatka Trajcheska, Marija Petkovska
 * @version 1.0 20.02.2013 
 * 
 * This class is splitting the input string into tokens that will be used in the next phases
 * of the validation of the input string using the grammar of the language for expressing the
 * guard of the transitions.
 */

public class TokenSplitting 
{
	static String input;
	static LinkedList<Token> tokens = new LinkedList<Token>();
	
	/**
	 * This method is the constructor of the class.
	 * 
	 * @param String input - the input string that should be split into tokens.
	 */
	public TokenSplitting(String input) 
	{
		TokenSplitting.input = input;
		TokenSplitting.input = TokenSplitting.input.replaceAll("\\s", "");
		tokens.clear();
	}
	
		
	/**
	 * This method is the one which splits the input string into tokens.
	 * 
	 * @return LinkedList<String> - list of tokens
	 */
	public LinkedList<Token> getTokens()
	{
		String tmp="";
		
		//check if the character is allowed 
		for(int i=0;i<input.length();i++)
		{
			if((((int)input.charAt(i)>=0) && ((int)input.charAt(i)<=47) ||
			   ((int)input.charAt(i)>=58) && ((int)input.charAt(i)<=64) ||
			   ((int)input.charAt(i)>=123) && ((int)input.charAt(i)<=127))
			   &&
			   ((int)input.charAt(i)!='+')&&
			   ((int)input.charAt(i)!='-')&&
			   ((int)input.charAt(i)!=';')&&
			   ((int)input.charAt(i)!='=')
			  )
			{

				return null;
			}
			   
			   
			switch(input.charAt(i))
			{
			
				// **************** + ****************
			    // ***********************************
				case '+':
				{
					if(tmp!="")
					{
						try 
						{
							Integer.parseInt(tmp);
							tokens.add(new Token("constant", tmp));
						}
						catch (Exception e) 
						{
							tokens.add(new Token("variable", tmp));
							//guard_grammar.Test.symbolTable.insertVariable(tmp);
						}
						tmp="";
					}
					tokens.add(new Token("+", "+"));
				}
				break;
				
				// **************** - ****************
				// ***********************************
				case '-':
				{
					if(tmp!="")
					{
						try 
						{
							Integer.parseInt(tmp);
							tokens.add(new Token("constant", tmp));
						}
						catch (Exception e) 
						{
							tokens.add(new Token("variable", tmp));
							//guard_grammar.Test.symbolTable.insertVariable(tmp);
						}
						tmp="";
					}
					tokens.add(new Token("-", "-"));
				}
				break;
				
				// **************** ; ****************
				// ***********************************
				case ';':
				{
					if(tmp!="")
					{
						try 
						{
							Integer.parseInt(tmp);
							tokens.add(new Token("constant", tmp));
						}
						catch (Exception e) 
						{
							tokens.add(new Token("variable", tmp));
							//guard_grammar.Test.symbolTable.insertVariable(tmp);
						}
						tmp="";
					}
					tokens.add(new Token(";", ";"));
				}
				break;
				
				// **************** = ****************
				// ***********************************
				case '=':
				{
					if(tmp!="")
					{
						try 
						{
							Integer.parseInt(tmp);
							tokens.add(new Token("constant", tmp));
						}
						catch (Exception e) 
						{
							tokens.add(new Token("variable", tmp));
							//guard_grammar.Test.symbolTable.insertVariable(tmp);
						}
						tmp="";
					}
					tokens.add(new Token("=", "="));
				}
				break;
								
				// **************** var/num ****************
				default:
				{
					tmp+=input.charAt(i);									
				}
				break;				
			}
		}
		
		if(tmp!="")
		{
			try 
			{
				Integer.parseInt(tmp);
				tokens.add(new Token("constant", tmp));
			}
			catch (Exception e) 
			{
				tokens.add(new Token("variable", tmp));
				//guard_grammar.Test.symbolTable.insertVariable(tmp);
			}
			tmp="";
		}
		return tokens;
	}
}
